using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.Spi;

namespace Aurora.TaskScheduler.Extension.BaseServices;

public class ASPDIJobFactory : IJobFactory
{
    private readonly IServiceScopeFactory _serviceProvider;
    public ASPDIJobFactory(IServiceScopeFactory serviceScopeFactory)
    {
        _serviceProvider = serviceScopeFactory;
    }
    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        using (var sevice = _serviceProvider.CreateScope())
        {
            //  var sevice = _serviceProvider.CreateScope();
            return sevice.ServiceProvider.GetService(bundle.JobDetail.JobType) as IJob;
        }
    }

    public void ReturnJob(IJob job)
    {
        (job as IDisposable)?.Dispose();
    }
}